package com.ruoyi.system.Fdongbei.service.impl;


import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.Fdongbei.domain.TbGbang;
import com.ruoyi.system.Fdongbei.domain.TbGuozhong;
import com.ruoyi.system.Fdongbei.domain.vo.BarDataVo;
import com.ruoyi.system.Fdongbei.mapper.DpMapper;
import com.ruoyi.system.Fdongbei.service.DpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;


@Service
@DataSource(DataSourceType.LY002_L)
public class DpServiceImpl implements DpService {
    @Autowired
    private DpMapper dpMapper;
    @Override
    public List<BarDataVo> selectCountMz(String period) {
        String startTime = "";
        String endTime = "";
        switch (period){
            case "now":
                //获取当前时间
                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDateTime now = LocalDateTime.now();
                String format = dtf.format(now);
                startTime = format + " 00:00:00";
                endTime = format + " 23:59:59";
                break;
            case "month":
                DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM");
                LocalDateTime now1 = LocalDateTime.now();
                int daysInMonth = YearMonth.from(now1).lengthOfMonth();
                String format1 = dtf1.format(now1);
                startTime = format1 + "-01 00:00:00";
                endTime = format1 + "-" + daysInMonth + " 23:59:59";
                break;
            case "year":
                DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy");
                LocalDateTime now2 = LocalDateTime.now();
                String format2 = dtf2.format(now2);
                startTime = format2 + "-01-01 00:00:00";
                endTime = format2 + "-12-31 23:59:59";
                break;
            default:
                return Collections.emptyList();
        }
        List<BarDataVo> barDataVo = dpMapper.selectCountMz(startTime,endTime);
        return barDataVo;
    }

    @Override
    public List<BarDataVo> selectZcp(String period) {
        String startTime = "";
        String endTime = "";
        switch (period){
            case "now":
                //获取当前时间
                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDateTime now = LocalDateTime.now();
                String format = dtf.format(now);
                startTime = format + " 00:00:00";
                endTime = format + " 23:59:59";
                break;
            case "month":
                DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM");
                LocalDateTime now1 = LocalDateTime.now();
                int daysInMonth = YearMonth.from(now1).lengthOfMonth();
                String format1 = dtf1.format(now1);
                startTime = format1 + "-01 00:00:00";
                endTime = format1 + "-" + daysInMonth + " 23:59:59";
                break;
            case "year":
                DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy");
                LocalDateTime now2 = LocalDateTime.now();
                String format2 = dtf2.format(now2);
                startTime = format2 + "-01-01 00:00:00";
                endTime = format2 + "-12-31 23:59:59";
                break;
            default:
                return Collections.emptyList();
        }
        List<BarDataVo> barDataVo = dpMapper.selectZcp(startTime,endTime);
        return barDataVo;
    }

    @Override
    public List<BarDataVo> selectQt(String period) {
        String startTime = "";
        String endTime = "";
        switch (period){
            case "now":
                //获取当前时间
                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDateTime now = LocalDateTime.now();
                String format = dtf.format(now);
                startTime = format + " 00:00:00";
                endTime = format + " 23:59:59";
                break;
            case "month":
                DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM");
                LocalDateTime now1 = LocalDateTime.now();
                int daysInMonth = YearMonth.from(now1).lengthOfMonth();
                String format1 = dtf1.format(now1);
                startTime = format1 + "-01 00:00:00";
                endTime = format1 + "-" + daysInMonth + " 23:59:59";
                break;
            case "year":
                DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy");
                LocalDateTime now2 = LocalDateTime.now();
                String format2 = dtf2.format(now2);
                startTime = format2 + "-01-01 00:00:00";
                endTime = format2 + "-12-31 23:59:59";
                break;
            default:
                return Collections.emptyList();
        }
        List<BarDataVo> barDataVo = dpMapper.selectQt(startTime,endTime);
        return barDataVo;
    }

    @Override
    public TbGbang selectGuobang(String period) {
        String startTime = "";
        String endTime = "";
        switch (period){
            case "now":
                //获取当前时间
                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDateTime now = LocalDateTime.now();
                String format = dtf.format(now);
                startTime = format + " 00:00:00";
                endTime = format + " 23:59:59";
                break;
            case "month":
                DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM");
                LocalDateTime now1 = LocalDateTime.now();
                int daysInMonth = YearMonth.from(now1).lengthOfMonth();
                String format1 = dtf1.format(now1);
                startTime = format1 + "-01 00:00:00";
                endTime = format1 + "-" + daysInMonth + " 23:59:59";
                break;
            case "year":
                DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy");
                LocalDateTime now2 = LocalDateTime.now();
                String format2 = dtf2.format(now2);
                startTime = format2 + "-01-01 00:00:00";
                endTime = format2 + "-12-31 23:59:59";
                break;
            default:
                return null;
        }
        TbGbang tbGbang = dpMapper.selectZcount(startTime,endTime);
        TbGbang tbGbang1 = dpMapper.selectJcbang(startTime,endTime);
        if(tbGbang1 != null && tbGbang1.getJbang() != null && tbGbang1.getCbang() != null){
            if(tbGbang1.getJbang()!= null && tbGbang1.getCbang()!= null){
                Double jbang = tbGbang1.getJbang();
                Double cbang = tbGbang1.getCbang();
                tbGbang.setJbang(jbang);
                tbGbang.setCbang(cbang);
            }
        }

        return tbGbang;
    }

    @Override
    public List<TbGuozhong> selectJinchu(String period) {
        String startTime = "";
        String endTime = "";
        switch (period){
            case "now":
                //获取当前时间
                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDateTime now = LocalDateTime.now();
                String format = dtf.format(now);
                startTime = format + " 00:00:00";
                endTime = format + " 23:59:59";
                break;
            case "month":
                DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM");
                LocalDateTime now1 = LocalDateTime.now();
                int daysInMonth = YearMonth.from(now1).lengthOfMonth();
                String format1 = dtf1.format(now1);
                startTime = format1 + "-01 00:00:00";
                endTime = format1 + "-" + daysInMonth + " 23:59:59";
                break;
            case "year":
                DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy");
                LocalDateTime now2 = LocalDateTime.now();
                String format2 = dtf2.format(now2);
                startTime = format2 + "-01-01 00:00:00";
                endTime = format2 + "-12-31 23:59:59";
                break;
            default:
                return Collections.emptyList();
        }

        List<TbGuozhong> tbGuozhong = dpMapper.selectJinchu(startTime,endTime);
        return tbGuozhong;
    }

    @Override
    public List<BarDataVo> selectCountMzR(String period) {
        String startTime = "";
        String endTime = "";
        switch (period){
            case "now":
                //获取当前时间
                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDateTime now = LocalDateTime.now();
                String format = dtf.format(now);
                startTime = format + " 00:00:00";
                endTime = format + " 23:59:59";
                break;
            case "month":
                DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM");
                LocalDateTime now1 = LocalDateTime.now();
                int daysInMonth = YearMonth.from(now1).lengthOfMonth();
                String format1 = dtf1.format(now1);
                startTime = format1 + "-01 00:00:00";
                endTime = format1 + "-" + daysInMonth + " 23:59:59";
                break;
            case "year":
                DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy");
                LocalDateTime now2 = LocalDateTime.now();
                String format2 = dtf2.format(now2);
                startTime = format2 + "-01-01 00:00:00";
                endTime = format2 + "-12-31 23:59:59";
                break;
            default:
                return Collections.emptyList();
        }
        List<BarDataVo> barDataVo = dpMapper.selectCountMzR(startTime,endTime);
        return barDataVo;
    }

    @Override
    public List<BarDataVo> selectZcpR(String period) {
        String startTime = "";
        String endTime = "";
        switch (period){
            case "now":
                //获取当前时间
                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDateTime now = LocalDateTime.now();
                String format = dtf.format(now);
                startTime = format + " 00:00:00";
                endTime = format + " 23:59:59";
                break;
            case "month":
                DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM");
                LocalDateTime now1 = LocalDateTime.now();
                int daysInMonth = YearMonth.from(now1).lengthOfMonth();
                String format1 = dtf1.format(now1);
                startTime = format1 + "-01 00:00:00";
                endTime = format1 + "-" + daysInMonth + " 23:59:59";
                break;
            case "year":
                DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy");
                LocalDateTime now2 = LocalDateTime.now();
                String format2 = dtf2.format(now2);
                startTime = format2 + "-01-01 00:00:00";
                endTime = format2 + "-12-31 23:59:59";
                break;
            default:
                return Collections.emptyList();
        }
        List<BarDataVo> barDataVo = dpMapper.selectZcpR(startTime,endTime);
        return barDataVo;
    }

    @Override
    public List<BarDataVo> selectQtR(String period) {
        String startTime = "";
        String endTime = "";
        switch (period){
            case "now":
                //获取当前时间
                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDateTime now = LocalDateTime.now();
                String format = dtf.format(now);
                startTime = format + " 00:00:00";
                endTime = format + " 23:59:59";
                break;
            case "month":
                DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM");
                LocalDateTime now1 = LocalDateTime.now();
                int daysInMonth = YearMonth.from(now1).lengthOfMonth();
                String format1 = dtf1.format(now1);
                startTime = format1 + "-01 00:00:00";
                endTime = format1 + "-" + daysInMonth + " 23:59:59";
                break;
            case "year":
                DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy");
                LocalDateTime now2 = LocalDateTime.now();
                String format2 = dtf2.format(now2);
                startTime = format2 + "-01-01 00:00:00";
                endTime = format2 + "-12-31 23:59:59";
                break;
            default:
                return Collections.emptyList();
        }
        List<BarDataVo> barDataVo = dpMapper.selectQtR(startTime,endTime);
        return barDataVo;
    }
}